package com.ckx.blog.controller;

import com.ckx.domain.ResponseResult;

import com.ckx.domain.entity.User;
import com.ckx.domain.vo.BlogUserLoginVo;
import com.ckx.enums.AppHttpCodeEnum;
import com.ckx.exception.SystemException;
import com.ckx.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class BlogLoginController {

    @Autowired
    private LoginService loginService;

    @PostMapping("/login")
    public ResponseResult login(@RequestBody @Validated User user){
        //todo 后期使用注解 绑定校验
        //数据校验
        if (!StringUtils.hasText(user.getUserName()) || !StringUtils.hasText(user.getPassword())){
            throw new SystemException(AppHttpCodeEnum.REQUIRE_BLANK_SUBMIT);
        }
        BlogUserLoginVo loginVo =  loginService.login(user);
        return ResponseResult.okResult(loginVo);

    }

    @PostMapping("/logout")
    public ResponseResult logout(){
        loginService.logout();
        return ResponseResult.okResult();
    }

}
